<?php

/**
* @current userinfo.php
* @author   LiuYang <liu_yang@haibao.com>
* @date 2016年4月27日 上午10:11:24
*/

namespace haibao\user\business;

use \haibao\user\model\filter\UserAttentionRecord as UserAttentionRecordFilter;
use \haibao\user\model\filter\UserReg as UserRegFilter;
use haibao\frame\data\query\Condition;
use haibao\app\business\BusinessException;

class UserInfo extends \haibao\user\business\Base{
    private static $instance = null;
	
    const FROMTYPE_FRIEND = 'friends';
    const FROMTYPE_FANS = 'fans';

    public static function updateBackground($userId,$imageId,$imageUrl){
    	if (!$userId){
    		throw new BusinessException('用户id不能为空');
    	}
    	if (!$imageId){
    		throw new BusinessException('图片id不能为空');
    	}
    	if (!$imageUrl){
    		throw new BusinessException('图片url不能为空');
    	}
    	
    	$data = new \haibao\user\data\UserInfo();
    	
    	$data->updateBackground($userId,$imageId,$imageUrl);
    }
    
    public static function getAttentionCount($userId){
    	if (!$userId){
    		throw new BusinessException('用户id不能为空');
    	}
    	
    	return self::getInstance()->getAttentionCount($userId);
    }
    
    public static function getUserScore($userId){
    	if (!$userId){
    		throw new BusinessException('用户id不能为空');
    	}
    	$userIntegralData = new \haibao\user\data\UserIntegral();
    	
    	$userIntegral = new \haibao\user\model\filter\UserIntegral();
    	$userIntegral->select(array(
    			$userIntegral::CONDITION_FIELD_NAME_ID,
    			$userIntegral::CONDITION_FIELD_NAME_USER_ID,
    			$userIntegral::CONDITION_FIELD_NAME_INTEGRALS
    	));
    	$userIntegral->where($userIntegral::CONDITION_FIELD_NAME_USER_ID, Condition::CONDITION_EQUAL, (int)$userId);
    	$userIntegralInfo = $userIntegralData->getOne($userIntegral);
    	if($userIntegralInfo){
    		return $userIntegralInfo->Integrals;
    	}
    	return 0;
//     	return self::getInstance()->getUserScore($userId);
    }
    
    public static function getUserLogin($userId){
    	if (!$userId){
    		throw new BusinessException('用户id不能为空');
    	}
    	
    	return self::getInstance()->getUserLogin($userId);
    }
    
    public static function delMessageBoard($id,$loginUserId,$currentUserId){
    	if (!$id){
    		throw new BusinessException('id不能为空');
    	}
    	if (!$loginUserId || !$currentUserId){
    		throw new BusinessException('用户id不能为空');
    	}
    	
    	$filter = new \haibao\user\model\filter\UserMessageBox();
    	$filter->where(
    			\haibao\user\model\filter\UserMessageBox::CONDITION_FIELD_NAME_ID,
    			Condition::CONDITION_EQUAL,
    			$id
    	);
    	$model = self::getInstance()->getOne($filter);
    	
    	if (!$model){
    		throw new BusinessException('参数错误');
    	}
    	
    	//留言只有空间主人能删除,回复只有创建人和空间主人能删
    	$isAllow = true;
    	if (!$model->ReplyId && $loginUserId != $model->UserId){
    		$isAllow = false;
    	}else if ($model->ReplyId && $loginUserId != $model->SendUserId && $loginUserId != $currentUserId){
    		$isAllow = false;
    	}
    	
    	if (!$isAllow){
    		throw new BusinessException('权限不足');
    	}
    	
    	self::getInstance()->delMessageBoard($model);
    }
    
    public static function addMessageBoard($model){
    	if (!$model->UserId || !$model->SendUserId){
    		throw new BusinessException('用户id不能为空');	
    	}
    	if (!$model->Content){
    		throw new BusinessException('内容不能为空');
    	}
    	
    	self::getInstance()->add($model);
    }
    
    public static function searchUserMessageBox($filter){
    	return self::getInstance()->getAll($filter);
    }
    
    public static function addVisitCount($userId){
    	self::getInstance()->addVisitCount($userId);
    }
    
    public static function saveLastGuest($userId,$guestInfo){
    	self::getInstance()->saveLastGuest($userId,$guestInfo);
    }
    
    public static function getLastGuest($userId){
    	return self::getInstance()->getLastGuest($userId);
    }
    
    public static function setTodayGuestCount($userId){
    	self::getInstance()->setTodayGuestCount($userId);
    }
    
    public static function getTodayGuestCount($userId){
    	return self::getInstance()->getTodayGuestCount($userId);
    }
    
    public static function getUserProfileById($id){
    	return self::getInstance()->getUserProfileById($id);
    }
    

    /**
     * 
     */
    public static function getBloggerByTime($userId){
        return self::getInstance()->getBloggerByTime($userId);
    }
    

    
    /**
     * 
     */
    public static function getUserByLoginTime($userId){
        return self::getInstance()->getUserByLoginTime($userId);
    }
    
    /**
     * 我的好友，我的粉丝
     * @param $userAttentionRecordFilter
     */
	public static function getFriendList($userAttentionRecordFilter){
	    $data = new \haibao\user\data\UserInfo();
	    return  $data->getAll($userAttentionRecordFilter);
	}
	
	public static function getOneUserByFilter($userFilter){
	    $data = new \haibao\user\data\UserInfo();
	    return  $data->getOne($userFilter);
	}
	
	/**
	 *我的博文 
	 * @param $blogFilter
	 */
	public static function getBlogList($blogFilter){
	    $data = new \haibao\user\data\Blog();
	    return  $data->getAll($blogFilter);
	}
	
	/**
	 * 我的帖子
	 * @param $threadFilter
	 */
    public static function getThreadList($threadFilter){
	    $data = new \haibao\user\data\Thread();
	    return  $data->getAll($threadFilter);
	}
	
	/**
	* 添加收货地址
	*/
	public static function addUserAddress($model){
		$data = new \haibao\user\data\UserAddress();
		$model->UpdateTime = new \DateTime();
		return $data->add($model);
	}
	
	/**
	 * 我的收货地址
	 */
	public static function getUserAddress($filter,$isOne=false){
		$data = new \haibao\user\data\UserAddress();
		if($isOne){
			return $data->getOne($filter);
		}else{
			return $data->getAll($filter);
		}
	}
	
	/**
	 * 通过地址ID获取
	 */
	public static function getOneByAddressId($addressId){
	    $data = new \haibao\user\data\UserAddress();
	    return $data->getOneById($addressId);
	}
	
	/**
	* 设置默认地址
	*/
	public static function updateAddressById($id,$arrSet){
		$data = new \haibao\user\data\UserAddress();
		return $data->updateById($id,$arrSet);
	}
	
	/**
	* 更新用户地址
	*/
	public static function updateUserAddress($arrSet,$filter){
		$data = new \haibao\user\data\UserAddress();
		return $data->update($arrSet,$filter);
	}
	
	/**
	 * 他的关系，包括他的好友 他的粉丝
	 */
	public static function getHisRelative($userId){
	    
	   $friendList = $fansList = array();
	   
	   $friendList = self::paceModel($userId,self::FROMTYPE_FRIEND);
	   
	   $fansList = self::paceModel($userId,self::FROMTYPE_FANS);
	   
	   return array(self::recombineData($friendList),self::recombineData($fansList));
	}
	
	public static function setBackGround($userId,$imageId,$imageUrl){
		self::getInstance()->setBackGround($userId,$imageId,$imageUrl);
	}
	
	public static function signIn($userId){
		if (!$userId){
			throw new BusinessException('用户id不能为空');
		}
		
		//判断是否已签到
		if (self::checkSignIn($userId)){
			return true;
		}
		
		$userData = new \haibao\user\data\User();
		$credit = $userData->addUserIntegral($userId,date('Y-m-d H:i:s'),\haibao\user\data\User::USER_INTEGRAL_SIGN);
		if($credit){
			$data = new \haibao\user\data\authuser();
			$data->signIn($userId,$credit);
		}
		
		return $credit;
	}
	
	public static function getSignCredit($userId,$time = null){
		$credit = null;
		if (!$userId){
			throw new BusinessException('用户id不能为空');
		}
		
		if (!$time){
			$time = date('Y-m-d');
		}
		
		$data = new \haibao\user\data\authuser();
		//1-2天：5分，3-5天：10分，6-7天：15积分，8天及以上：20积分
		$days = $data->getContinuousSignDays($userId,$time);
		$days = $days + 1;
		
		if ($days >= 8){
			$credit = 20;
		}else if ($days >=6 && $days <=7){
			$credit = 15;
		}else if ($days >=3 && $days <=5){
			$credit = 10;
		}else if ($days >=1 && $days <=2){
			$credit = 5;
		}
		
		return $credit;
	}
	
	public static function checkSignIn($userId,$time = null){
		if (!$userId){
			throw new BusinessException('用户id不能为空');
		}
		
		if (!$time){
			$time = date('Y-m-d');
		}
		
		$data = new \haibao\user\data\authuser();
		
		return $data->checkSignIn($userId,$time);
	}
	
	private static function getFriendShip($followUserId,$userId){
		$data = new \haibao\user\data\UserInfo();
		
		$data->getFriendShip($followUserId,$userId);
	}
	
	private static function paceModel($userId,$fromType){
	    $userAttentionRecordFilter = new UserAttentionRecordFilter();
	    $userRegFilter = new UserRegFilter();
	    $userAttentionRecordFilter->select(array(
	        UserAttentionRecordFilter::CONDITION_FIELD_NAME_ID,
	        UserAttentionRecordFilter::CONDITION_FIELD_NAME_USERID,
	        UserAttentionRecordFilter::CONDITION_FIELD_NAME_ATTENTION_USERID,
	        UserAttentionRecordFilter::CONDITION_FIELD_NAME_USERTYPE,
	        UserAttentionRecordFilter::CONDITION_FIELD_NAME_STATUS,
	    ))->limit(4);
	    
	    $userRegFilter->select(array(
	        UserRegFilter::CONDITION_FIELD_NAME_ID,
	        UserRegFilter::CONDITION_FIELD_NAME_AVATARURL,
	        UserRegFilter::CONDITION_FIELD_NAME_NICKNAME,
	        UserRegFilter::CONDITION_FIELD_NAME_USERNAME,
	    ));
	    
	    if($fromType == self::FROMTYPE_FRIEND){
	        $userAttentionRecordFilter->leftJoin($userRegFilter,null,UserAttentionRecordFilter::CONDITION_FIELD_NAME_ATTENTION_USERID,UserRegFilter::CONDITION_FIELD_NAME_ID, UserAttentionRecordFilter::CONDITION_FIELD_NAME_USERLIST);
	        $userAttentionRecordFilter->where(UserAttentionRecordFilter::CONDITION_FIELD_NAME_USERID, Condition::CONDITION_EQUAL, $userId);
	    }elseif ($fromType == self::FROMTYPE_FANS){
	        $userAttentionRecordFilter->leftJoin($userRegFilter,null,UserAttentionRecordFilter::CONDITION_FIELD_NAME_USERID,UserRegFilter::CONDITION_FIELD_NAME_ID, UserAttentionRecordFilter::CONDITION_FIELD_NAME_USERLIST);
	        $userAttentionRecordFilter->where(UserAttentionRecordFilter::CONDITION_FIELD_NAME_ATTENTION_USERID, Condition::CONDITION_EQUAL, $userId);
	    }
	    
	    $userAttentionRecordFilter->where(UserAttentionRecordFilter::CONDITION_FIELD_NAME_USERTYPE, Condition::CONDITION_EQUAL, \haibao\user\model\data\UserAttentionRecord::USERTYPE_USER );
	    $userAttentionRecordFilter->where(UserAttentionRecordFilter::CONDITION_FIELD_NAME_STATUS, Condition::CONDITION_EQUAL, \haibao\user\model\data\UserAttentionRecord::STATUS_ISOK);
	    
	    return  self::getFriendList($userAttentionRecordFilter);
	}
	
	private static function recombineData($data){
	    $dataList = array();
	    if($data){
	        foreach ($data as $key => $value){
	        	$nickName = '海报报友';
	        	if(!empty($value->UserList)){
	        		$nickName = $value->UserList[0]->NickName;
	        	}
	            array_push($dataList,array(
	            	'UserId'=>$value->UserId,
	            	'NickName'=>$nickName,
	            	'AttentionUserId'=>$value->AttentionUserId
	            ));
	        } 
	    }
	   
	   return $dataList; 
	}
	
	public static function delBlogById($blogId){
	    $data = new \haibao\user\data\Blog();
	    return  $data->removeById($blogId);
	}
	
	public static function getBlogProperty($userId,$isMySelf){
	    $data = new \haibao\user\data\Blog();
	    return  $data->getBlogProperty($userId,$isMySelf);
	}
	
	public static function getThreadPorperty($userId,$isMySelf){
	    $data = new \haibao\user\data\Thread();
	    return  $data->getThreadPorperty($userId,$isMySelf);
	}
	
	public static function checkUserLoginAddCredit($userId){
		$credit = 0;
		if($userId){
			\haibao\user\common\ScoreRule::operateScore($userId,\haibao\user\common\ScoreRule::TYPE_LOGIN);
		}
		return $credit;
	}
	private static function getInstance(){
		if (!self::$instance){
			self::$instance = new \haibao\user\data\UserInfo();
		}
		 
		return self::$instance;
	}
}

